package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.sgb.kjwl.BR;
import com.sgb.link.utils.PickCateType;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.LayoutBidingChooseBinding;
import com.sgb.link.view.adapter.PickAdapter;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.dialog.DialogViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import java.util.ArrayList;
import java.util.List;

public class PickChooseLeftDialog2 extends BaseBottomDialog {

    /**
     * 三级标题，通过category不同显示的不同
     */
    private String[][] titles = new String[][]{
            new String[]{"机械分类", "机械类型"},
            new String[]{"工程类别", "专业分类", "业务类别"},
            new String[]{"商品分类", "商品类别", "商品品名"},
            new String[]{"工程类别", "专业分类", "业务类型"},
            new String[]{"设备分类", "设备类型"},
            new String[]{"房产类型", "房产分类"},
            new String[]{"货物类型", "要求车型"},
    };


    // 缓存数据集
    private SparseArray<PickSelectTextEntity> mCacheData = new SparseArray<>();
    private SparseIntArray mCacheFilters = new SparseIntArray();
    private SparseArray<String> mNameArrays;

    private BiddingChildAdapter childAdapter;
    private DialogCallBackImpl mCallBack;
    private int mType = 0;
    private boolean isBid;       //是否是竞价
    private boolean isMap;
    private NestedScrollView nestedScrollView;

    public PickChooseLeftDialog2(Context context, SparseArray<String> nameArray) {
        super(context, com.swgk.core.R.style.dialog_base, null, Gravity.END);
        mNameArrays = nameArray;
        for (int i = 0; i < mNameArrays.size(); i++) {
            addEntity(i, new ArrayList<>());
        }
    }

    @Override
    protected int dialogLayout() {
        return R.layout.pick_choose_left_dialog;
    }

    @Override
    protected void initView(DialogViewHolder holder, Bundle savedInstanceState) {
        TextView resetView = holder.getDialog().findViewById(R.id.reset_view);
        TextView okView = holder.getDialog().findViewById(R.id.ok_view);
        nestedScrollView = holder.getDialog().findViewById(R.id.ne_sc);

        RecyclerView recyclerView = holder.getDialog().findViewById(R.id.type_list);
        childAdapter = new BiddingChildAdapter(context, null, new ItemAction() {
            @Override
            public void onItemAction(PickSelectTextEntity entity, int position, int childPos) {
                mCacheFilters.put(entity.getSort(), childPos);
                if (mCallBack != null) {
                    mCallBack.loadCategory(entity, childPos);
                }
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(childAdapter);

        //重置
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallBack != null) {
                    mCallBack.reset();
                }
            }
        });

        //确定
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallBack != null) {
                    mCallBack.loadData(mCacheData, mCacheFilters);
                }
                dismiss();
            }
        });

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (mCallBack != null) {
                    mCallBack.onCancel();
                }
            }
        });
    }

    /**
     * 清理关联项为空
     */
    public void clearOtherDataInfo(int sort) {
        switch (sort) {
            case PickCateType.TYPE_CATEGORY_0:
                addEntity(PickCateType.TYPE_CATEGORY_1, new ArrayList<>());
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_CATEGORY_1:
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_PROVINCE:
                addEntity(PickCateType.TYPE_CITY, new ArrayList<>());
                break;
        }
    }

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mCacheData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mCacheData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mCacheData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mCacheData.put(index, new PickSelectTextEntity(index, mNameArrays.get(index), list));
        }
        mCacheFilters.put(index, 0);
    }

    public void addEntity(int index, List<PickSelectEntity> list, String name) {
        if (mCacheData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mCacheData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mCacheData.get(index).setPickSelectEntityList(list);
            }
            mCacheData.get(index).setTypeName(name);
        } else {
            mCacheData.put(index, new PickSelectTextEntity(index, name, list));
        }
        mCacheFilters.put(index, 0);
    }

    public void setCallBack(DialogCallBackImpl callBack) {
        mCallBack = callBack;
    }

    /**
     * 显示对话框
     */
    public void showDialog() {
        if (mCacheData == null || mCacheData.size() == 0) {
            return;
        }
        List<PickSelectTextEntity> entities = new ArrayList<>();
        for (int i = 0; i < mCacheData.size(); i++) {
            int key = mCacheData.keyAt(i);
            PickSelectTextEntity datum = mCacheData.get(key);
            if ((isMap && (datum.getTypeName().equals("省份") || datum.getTypeName().equals("市区")))
                    || (!isMap && datum.getTypeName().equals("距离"))) {
                // 模式切换 取消选中
                mCacheFilters.put(datum.getSort(), 0);
                continue;
            }
            if (datum.getPickSelectEntityList() == null || datum.getPickSelectEntityList().size() == 0) {
                mCacheFilters.put(datum.getSort(), 0);
                continue;
            }
            if (datum.getTypeName().equals("全部")) {
                continue;
            }

            if (mType == 6 || mType == 7 || mType == 8 || mType == 9 || mType == 10) {
                if (datum.getSort() == 0) {
                    datum.setTypeName(titles[3][0]);
                } else if (datum.getSort() == 1) {
                    datum.setTypeName(titles[3][1]);
                } else if (datum.getSort() == 2) {
                    datum.setTypeName(titles[3][2]);
                }
            } else if (mType == 11 || mType == 15) {
                if (datum.getSort() == 0) {
                    datum.setTypeName("房产类型");
                }
                if (datum.getSort() == 1) {
                    datum.setTypeName("房产分类");
                }
            } else if (mType == 12 || mType == 18) {
                if (datum.getSort() == 0) {
                    datum.setTypeName("车型");
                }
            } else if (mType == 13) {
                if (datum.getSort() == 0) {
                    datum.setTypeName("工程类别");
                }
            } else if (mType == 14) {
                if (datum.getSort() == 0) {
                    datum.setTypeName("土地用途");
                }
            } else if (mType == 16) {
                if (datum.getSort() == 0) {
                    datum.setTypeName(titles[0][0]);
                }
                if (datum.getSort() == 1) {
                    datum.setTypeName(titles[0][1]);
                }
            } else if (mType == 17) {
                if (datum.getSort() == 0) {
                    datum.setTypeName(titles[4][0]);
                }
                if (datum.getSort() == 1) {
                    datum.setTypeName(titles[4][1]);
                }
            } else if (mType == 19) {
                if (datum.getSort() == 0) {
                    datum.setTypeName(titles[6][0]);
                }
                if (datum.getSort() == 1) {
                    datum.setTypeName(titles[6][1]);
                }
            }else {

                if (mType > 0 && mType != 20) {
                    if (datum.getSort() == 0) {
                        datum.setTypeName(titles[mType - 1][0]);
                    } else if (datum.getSort() == 1) {
                        datum.setTypeName(titles[mType - 1][1]);
                    } else if (datum.getSort() == 2) {
                        datum.setTypeName(titles[mType - 1][2]);
                    }
                }

//                if (isBid && mType == 1) {
//                    if (datum.getSort() == 0) {
//                        datum.setTypeName("机械/设备");
//                    } else if (datum.getSort() == 1) {
//                        if (mCacheFilters.get(0) == 2) {  //设备
//                            datum.setTypeName(titles[4][0]);
//                        } else if (mCacheFilters.get(0) == 1) {
//                            datum.setTypeName(titles[0][0]);
//                        }
//                    } else if (datum.getSort() == 2) {
//                        if (mCacheFilters.get(0) == 2) {  //设备
//                            datum.setTypeName(titles[4][1]);
//                        } else if (mCacheFilters.get(0) == 1) {
//                            datum.setTypeName(titles[0][1]);
//                        }
//                    }
//                } else {
//                    if (mType > 0 && mType != 20) {
//                        if (datum.getSort() == 0) {
//                            datum.setTypeName(titles[mType - 1][0]);
//                        } else if (datum.getSort() == 1) {
//                            datum.setTypeName(titles[mType - 1][1]);
//                        } else if (datum.getSort() == 2) {
//                            datum.setTypeName(titles[mType - 1][2]);
//                        }
//                    }
//                }
            }
            entities.add(datum);
        }
        /*Collections.sort(entities, (o1, o2) -> {
            int i = o1.getSort() - o2.getSort();
            if (i == 0) {
                return o1.getSort() - o2.getSort();
            }
            return i;
        });*/
        childAdapter.setDatas(entities);
        if (!isShowing()) {
            show();
        }
    }

    public void initData(SparseArray<PickSelectTextEntity> sparseArray, SparseIntArray map) {
        nestedScrollView.smoothScrollTo(0, 0);
        nestedScrollView.scrollTo(0, 0);
        mCacheFilters = map.clone();
        mCacheData = PickCopyUtils.deepCopy(sparseArray);
    }

    /**
     * 初始化分类名称
     *
     * @param category tab
     * @param isMap    是否地图
     */
    public void initCategory(int category, boolean isMap, boolean isBid) {
        if (category < 0) {
            category = 0;
        }
        mType = category;
        this.isBid = isBid;
        this.isMap = isMap;
    }

    public int getItemPos(int i) {
        return mCacheFilters.get(i);
    }

    public interface DialogCallBackImpl {

        void loadCategory(PickSelectTextEntity no, int firPos);

        void loadData(SparseArray<PickSelectTextEntity> data, SparseIntArray map);

        void reset();

        void onCancel();
    }

    private class BiddingChildAdapter extends BaseRecycleAdapter<PickSelectTextEntity> {

        ItemAction itemAction;

        public BiddingChildAdapter(Context context, List<PickSelectTextEntity> datas, ItemAction itemAction) {
            super(context, R.layout.layout_biding_choose, datas);
            this.itemAction = itemAction;
        }

        @Override
        public void convert(ItemViewHolder holder, final PickSelectTextEntity entity) {

        }

        @Override
        public void onBindViewHolder(ItemViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            PickSelectTextEntity entity = datas.get(position);
            holder.setBinding(BR.viewModel, entity);
            LayoutBidingChooseBinding testBinding = (LayoutBidingChooseBinding) holder.getmBinding();
            testBinding.tvCategory.setText(entity.getTypeName());
            updateChildRecycler(testBinding.rv, entity, position, mCacheFilters.get(entity.getSort()));
        }

        public void updateChildRecycler(RecyclerView recyclerView, PickSelectTextEntity parentEntity, int parentPos, int defItem) {
            PickAdapter mAdapter = new PickAdapter(getContext(), parentEntity.getPickSelectEntityList(), R.layout.pick_choose_left_child_item) {
                @Override
                public void onBindViewHolder(ItemViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    TextView textView = holder.itemView.findViewById(R.id.tv_name);
                    textView.setText(datas.get(position).getPickerViewText());
                    if (holder.getItem().isSelected()) {
                        textView.setBackgroundResource(R.color.color_main_30);
                        textView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.color_main));
                    } else {
                        textView.setBackgroundResource(R.color.color_main_bg);
                        textView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.color_tab_two));
                    }
                }
            };
            mAdapter.setItemSelect(true);
            mAdapter.setDefItem(defItem);
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
            recyclerView.setAdapter(mAdapter);
            mAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
                @Override
                public void onItemClick(PickSelectEntity entity, int position) {
                    if (itemAction != null) {
                        itemAction.onItemAction(parentEntity, parentPos, position);
                        showDialog();
                    }
                }
            });
        }
    }

    private interface ItemAction {
        void onItemAction(PickSelectTextEntity entity, int position, int childPos);
    }

}
